#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

#include "can_rx.h"
#include "mosquitto.h"
#include "linux/can.h"
typedef struct s_conn_para{
	char *hostname;
	char *pub_topic;
	char *clientId;
	char *username;
	char *password;
	int port;
	char payload[256];
	char *sub_topic;
	int  keepalive;
	int  qos;
}conn_para_t;

void mqtt_clean(struct mosquitto *mosq);
static void my_message_callback(struct mosquitto *mosq,void *obj, const struct mosquitto_message *msg);

int main(int argc, char **argv)
{
	struct mosquitto *mosq;
	int		 mid,mid1;
	int		 retain =  0;
	int		 session = true;
	struct can_frame received_frame;
	const char *interface_name = "can0";





	conn_para_t 	conn_para =  {
		"iot-06z00i9k03hcz6s.mqtt.iothub.aliyuncs.com",
		"/sys/h098jXhNP9d/mqtt/thing/event/property/post",
		"h098jXhNP9d.mqtt|securemode=2,signmethod=hmacsha256,timestamp=1720751526199|",
		"mqtt&h098jXhNP9d",
		"16aae5231f7378909fd972fa0212ad774e5e75eb19532d32ccca175232aa7143",
		1883,
		"",
		"/sys/h098jXhNP9d/mqtt/thing/event/property/post_reply",
		60,
		0
	};

	mosquitto_lib_init();

	mosq =  mosquitto_new(conn_para.clientId, session, NULL);

	if(mosquitto_username_pw_set(mosq, conn_para.username, conn_para.password) != MOSQ_ERR_SUCCESS)
	{
		printf("connect server failure:%s\n",strerror(errno));
		goto cleanup;
	}

	printf("mqtt connect  parameters set OK\n");

	if(mosquitto_connect(mosq, conn_para.hostname, conn_para.port, conn_para.keepalive)  != MOSQ_ERR_SUCCESS)
	{
		printf("connect server failure:  %s\n", strerror(errno));
		goto cleanup;
	}
	printf("connect ok\n");

	if(mosquitto_loop_start(mosq) !=  MOSQ_ERR_SUCCESS)
	{
		printf("mosquitto_loop_start_failure: %s", strerror(errno));
		goto cleanup;
	}


	//sub  messages callback
    	mosquitto_message_callback_set(mosq, my_message_callback);

	if (mosquitto_subscribe(mosq, &mid, conn_para.sub_topic, conn_para.qos) != MOSQ_ERR_SUCCESS)
    	{
        printf("mosquitto subsribe failure: %s", strerror(errno));
		goto cleanup;
   	 }
	printf("sub OK\n");

		while(1)
		{
		
			
		received_frame = receive_can_message(interface_name);
		snprintf(conn_para.payload, sizeof(conn_para.payload),"{\"method\":\"thing.service.property.set\",\"id\":\"10353468\",\"params\":{\"can_id\":%d,\"can_DLC\":%d,\"can_data\":%d},\"version\":\"1.0.0\"}",(int)received_frame.can_id, (int)received_frame.can_dlc, (int)received_frame.data[1]);
		  //snprintf(conn_para.payload, sizeof(conn_para.payload),"{\"method\":\"thing.service.property.set\",\"id\":\"10353468\",\"params\":{\"can_id\":1,\"can_DLC\":1,\"can_data\":1},\"version\":\"1.0.0\"}");

		if (mosquitto_publish(mosq, &mid1, conn_para.pub_topic, strlen(conn_para.payload), conn_para.payload, conn_para.qos, retain) != MOSQ_ERR_SUCCESS)
		{
			printf("mosquitto publish data failure: %s", strerror(errno));
			continue;
		}
		printf("pub messages: %s\n", conn_para.payload);
		sleep(3);
		}
cleanup:
    mqtt_clean(mosq);
    return 0;


}

void mqtt_clean(struct mosquitto *mosq)
{
    mosquitto_destroy(mosq);
    mosquitto_lib_cleanup;
}


static void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg)
{
    printf("sub messages: %s\n", (char *)msg->payload);

}
